﻿/*
 * Copyright © 2011 Tristan Reeves, treeves@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using NUnit.Framework;
using Spring.Context.Support;
using Sprinkles.Tests.DeploymentHosts;
using System;
using System.Linq;

namespace Sprinkles.Tests.UnitTests.ConstructorArgs
{
    [TestFixture]
    public class CreationTests_ManyArgs_Auto
    {
        private const string ROOT_HOST = "AutoArgumentsHost";
        private Type GetTypeForOrdinal(int ord)
        {
            switch (ord)
            {
                case 1:
                    return typeof(AutoArgumentsHost.Implementor1);
                case 2:
                    return typeof(AutoArgumentsHost.Implementor2);
                case 3:
                    return typeof(AutoArgumentsHost.Implementor3);
                case 4:
                    return typeof(AutoArgumentsHost.Implementor4);
                default:
                    return typeof(AutoArgumentsHost.Implementor5);
            }
        }
        private Type GetInterfaceTypeForOrdinal(int ord)
        {
            switch (ord)
            {
                case 1:
                    return typeof(AutoArgumentsHost.IInterface1);
                case 2:
                    return typeof(AutoArgumentsHost.IInterface2);
                case 3:
                    return typeof(AutoArgumentsHost.IInterface3);
                case 4:
                    return typeof(AutoArgumentsHost.IInterface4);
                default:
                    return typeof(AutoArgumentsHost.IInterface5);
            }
        }

        [Test]
        public void ValidateAutoObjectsArePresent([Values(1, 2, 3, 4, 5)]int howManyArgsToTest)
        {
            var ctx = new XmlApplicationContext(@"config\ConstructionConfig1.xml");
            var obj = ctx[ROOT_HOST + "." + howManyArgsToTest] as AutoArgumentsHost.X;
            Assert.That(obj.Objects.Count, Is.EqualTo(howManyArgsToTest));

            for (int i = 0; i < howManyArgsToTest; i++)
            {
                var item = obj.Objects[i];
                var _typ = GetTypeForOrdinal(i + 1);
                var _ityp = GetInterfaceTypeForOrdinal(i + 1);

                Assert.That(item.Name, Is.EqualTo("Name" + (i + 1)));
                Assert.That(_typ, Is.EqualTo(item.GetType()));
                Assert.That(_typ.GetInterfaces(), Contains.Item(_ityp));
            }
        }
    }
}
